package com.hadluo.qq.client.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Properties;

public class ConfigManager {
    private static final String CONFIG_DIR = "conf";
    private static final String CONFIG_FILE = "login.properties";

    private static final String QQ_NUMBER_KEY = "qq_number";
    private static final String PASSWORD_KEY = "password";

    private static Properties properties;
    private static Path configPath;

    static {
        try {
            // 确保配置目录存在
            Path confDir = Paths.get(CONFIG_DIR);
            if (!Files.exists(confDir)) {
                Files.createDirectory(confDir);
            }

            // 初始化配置文件路径
            configPath = confDir.resolve(CONFIG_FILE);
            if (!Files.exists(configPath)) {
                Files.createFile(configPath);
            }

            // 加载配置
            properties = new Properties();
            try (InputStream input = Files.newInputStream(configPath)) {
                properties.load(input);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 保存登录信息
    public static void saveLoginInfo(String qqNumber, String password) {
        try {
            properties.setProperty(QQ_NUMBER_KEY, qqNumber);
            properties.setProperty(PASSWORD_KEY, password);

            try (OutputStream output = Files.newOutputStream(configPath)) {
                properties.store(output, "QQ Client Login Information");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取保存的QQ号
    public static String getSavedQQNumber() {
        return properties.getProperty(QQ_NUMBER_KEY, "");
    }

    // 获取保存的密码
    public static String getSavedPassword() {
        return properties.getProperty(PASSWORD_KEY, "");
    }
}